#include "syncserver.h"



SyncServer::SyncServer() {	
    ui = new Ui_SyncServer();
    ui->setupUi(this);
	ui->console->setAutoFillBackground(true);
    connect(ui->powerButton, SIGNAL(clicked()),
		this, SLOT(powerSwitch()));

	
	powerOn = false;


	syncManager = new SyncManager();
	syncManager->setHomeDir("Documents_PC");
	connect(syncManager, SIGNAL(statusSignal(int)),
			this, SLOT(updateStatus(int)));
	updateStatus(SyncManager::STOP);
	showPhoneBookDialog();
}

SyncServer::~SyncServer() {
    delete ui;
	if (phoneBookDialog != NULL) {
		phoneBookDialog->~PhoneBookDialog();
	}
}

void SyncServer::powerSwitch() {
	if (powerOn) {	
		//===== Turn Off
		syncManager->stop();
	} else {
		//===== Turn On
		QString ipStr = ui->ipLineEdit->text();
		QString portStr = ui->portLineEdit->text();
		QString result = "";
		if (!SyncManager::validateIp(ipStr)) {
			result += "Invalid Ip address\n";
		}
		if (!SyncManager::validatePort(portStr)) {
			result += "Invalid port number\n";
		}
		if (result.length() != 0) {
			QMessageBox::warning(this, "Warning.",
			result, QMessageBox::Ok);
		} else {
			syncManager->listen(ipStr, portStr);
		}
    }
}


void SyncServer::phoneBookDialogCloseListener() {
}

void SyncServer::updateStatus(int status) {
	switch (status) {
	case SyncManager::LISTEN:
		powerOn = true;
		ui->ipLineEdit->setEnabled(false);
		ui->portLineEdit->setEnabled(false);
		ui->statusLabel->setText("Listening...");
    	ui->console->setText("Waiting for connection");
		ui->powerButton->setText("stop");
		break;
	case SyncManager::STOP:
		powerOn = false;
		ui->ipLineEdit->setEnabled(true);
		ui->portLineEdit->setEnabled(true);
		ui->powerButton->setText("start");
		ui->statusLabel->setText("Stop");
		ui->console->setText("Server stoped.");
		break;
	case SyncManager::ERROR_NETWORK:
		powerOn = false;
		if (phoneBookDialog != NULL) {
			phoneBookDialog->close();
			phoneBookDialog = NULL;
		}
		ui->statusLabel->setText("Listening...");
		ui->console->setText("Connection abort.");
		break;
	case SyncManager::CONNECT:
		ui->statusLabel->setText("Connect");
		ui->console->setText("Synchronising...");
		delete phoneBookDialog;
		break;
	case SyncManager::SYNCED:
		ui->statusLabel->setText("Connect");
		ui->console->setText("Synchronised.");
		showPhoneBookDialog();	
		break;
	default:
		break;
	}
}

void SyncServer::showPhoneBookDialog() {
	phoneBookDialog = new PhoneBookDialog();
	phoneBookDialog->setPalette(Qt::gray);
	//===== dialog -> syncManager
	connect(phoneBookDialog, SIGNAL(addContactSignal(NeoPhoneBookEntry*)),
			syncManager, SLOT(addContactListener(NeoPhoneBookEntry*)));
	connect(phoneBookDialog, SIGNAL(replaceContactSignal(int , NeoPhoneBookEntry*)),
			syncManager, SLOT(replaceContactListener(int, NeoPhoneBookEntry*)));
	connect(phoneBookDialog, SIGNAL(deleteContactSignal(int)),
			syncManager, SLOT(deleteContactListener(int)));
	connect(phoneBookDialog, SIGNAL(callContactSignal(int)),
			syncManager, SLOT(callContactListener(int)));
	connect(phoneBookDialog, SIGNAL(stopCallContactSignal()),
			syncManager, SLOT(stopCallContactListener()));

	//===== syncManager -> dialog
	connect(syncManager, SIGNAL(addContactSignal(NeoPhoneBookEntry*)),
			phoneBookDialog, SLOT(addContactListener(NeoPhoneBookEntry*)));
	connect(syncManager, SIGNAL(replaceContactSignal(int , NeoPhoneBookEntry*)),
			phoneBookDialog, SLOT(replaceContactListener(int, NeoPhoneBookEntry*)));
	connect(syncManager, SIGNAL(deleteContactSignal(int)),
			phoneBookDialog, SLOT(deleteContactListener(int)));
	connect(syncManager, SIGNAL(callContactSignal(int)),
			phoneBookDialog, SLOT(callContactListener(int)));
	connect(syncManager, SIGNAL(stopCallContactSignal()),
			phoneBookDialog, SLOT(stopCallContactListener()));

	//dialog->setAttribute(Qt::WA_DeleteOnClose);
	phoneBookDialog->show();
}






